package com.gopay.checkorder.service.task.thread;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;

import com.gopay.gass.service.GassPayRemoteService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.checkorder.service.BankOrderService;
import com.gopay.checkorder.service.alarm.OrderAlarmDataCollector;
import com.gopay.checkorder.service.sentinel.BcdlOrderSentinel;
import com.gopay.common.constants.bank.BankOrderStatus;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BankOrder;
import com.gopay.common.domain.cps.vo.CheckOrderResult;
import com.gopay.common.util.HttpTransport;

import javax.annotation.Resource;

public class CheckOrderBcdlThread implements Runnable{
	private Logger logger = LoggerFactory.getLogger(this.getClass());

    private BankOrderService bankOrderService;
    private ConcurrentLinkedQueue<BankOrder> queue;
    private CountDownLatch latch;
    private GassPayRemoteService gassPayService;
    private BcdlOrderSentinel sentinel;
    
    private OrderAlarmDataCollector alarmCollector;
	public CheckOrderBcdlThread(BcdlOrderSentinel sentinel,BankOrderService bankOrderService, ConcurrentLinkedQueue<BankOrder> queue,
            CountDownLatch latch,OrderAlarmDataCollector alarmCollector,GassPayRemoteService gassPayService){
		this.sentinel = sentinel;
        this.bankOrderService = bankOrderService;
        this.queue = queue;
        this.latch = latch;
        this.alarmCollector=alarmCollector;
        this.gassPayService = gassPayService;
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
        BankOrder bankOrder = null;
        CheckOrderResult checkResult = null;
        HttpTransport notifyGass = new HttpTransport();
        notifyGass.setSendEncoding("UTF-8");
        while ((bankOrder = queue.poll()) != null) {
            try {
                alarmCollector.addTotal(bankOrder.getBankCode());
                
                checkResult=bankOrderService.checkOrder(bankOrder, BankOrderStatus.AUTO_CHECK_SUCCESS);
                if(checkResult!=null && checkResult.isCommunicateError()==true){
                    logger.error("checkResult  communicateError-----------"+checkResult.isCommunicateError());
                    alarmCollector.addFail(bankOrder.getBankCode());
                    logger.error("checkResult  fail*****"+alarmCollector.getFail(bankOrder.getBankCode()));
                    logger.error("checkResult  total****"+alarmCollector.getTotal(bankOrder.getBankCode()));
                }
                
                Map<String, String> param = new HashMap<String, String>();
				if (ProcCodeConstants.PROC_CODE_100E6027.equals(checkResult.getErrCode())) {
					param.put("resultCode", "2");
                    //delete by zyt,2017-8-4，删除remark标志
                    // param.put("remark", "2");
					//add by zyt,2017-10-13 ,是否需要做失败的拒绝账务，
					if(checkResult.isNeedRefund()){
						logger.info("发起失败拒绝账务：orderId:" + bankOrder.getOrderId());
						param.put("needRefund", "1");
					}
					param.put("resultMsg", checkResult.getErrMsg());
					param.put("gopayOrderId", bankOrder.getOrderId());
					/*notifyGass.setUrl(bankOrder.getRemark2());
					notifyGass.submit(param);*/ //生产优化10期 lx httpclient 替换为 hessian
                    gassPayService.bcdlNotifyGass(param);
				}else if(checkResult.isSuccess()){
					param.put("resultCode", "1");
                    //delete by zyt,2017-8-4，删除remark标志
                    // param.put("remark", "2");
					param.put("gopayOrderId", bankOrder.getOrderId());
					/*notifyGass.setUrl(bankOrder.getRemark2());
					notifyGass.submit(param);*/ //生产优化10期 lx httpclient 替换为 hessian
                    gassPayService.bcdlNotifyGass(param);
				}else{
					param.put("resultCode", "3");
                    //delete by zyt,2017-8-4，删除remark标志
                    // param.put("remark", "2");
					param.put("gopayOrderId", bankOrder.getOrderId());
					/*notifyGass.setUrl(bankOrder.getRemark2());
					notifyGass.submit(param);*/ //生产优化10期 lx httpclient 替换为 hessian
                    gassPayService.bcdlNotifyGass(param);
				}
                
                logger.info("**********************当前队列剩余订单数：" + queue.size());
            } catch (Exception e) {
                logger.info("---------- 对单error---------", e);
            } finally {
            	logger.error("--------调用熔断器开始---------");
				sentinel.checkPartol(bankOrder,checkResult);
				logger.error("--------调用熔断器结束---------");
                latch.countDown();
            }
        }
	}
}
